package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.Visitor;
import com.itheima.domain.vo.NearUserVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.RecommendUserVo;
import com.itheima.domain.vo.VisitorVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLocationService;
import com.itheima.service.mongo.VisitorService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MakeFriendManager {
    @DubboReference
    private RecommendUserService recommendUserService;
    @DubboReference
    private UserInfoService userInfoService;

    public ResponseEntity todayBest() {
        Long localUserId = UserHolder.getLocalUserId();
        RecommendUser recommendUser = recommendUserService.findTodayBest(localUserId);
        //新用户封装假数据
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(99L);
            recommendUser.setScore(80.0);
            recommendUser.setToUserId(localUserId);
        }
        UserInfo userInfo = userInfoService.findByUserId(recommendUser.getUserId());

        RecommendUserVo recommendUserVo = new RecommendUserVo();
        recommendUserVo.setUserInfo(userInfo);
        recommendUserVo.setFateValue(recommendUser.getScore().longValue());
        return ResponseEntity.ok(recommendUserVo);
    }

    public ResponseEntity recommendation(Integer page, Integer pagesize) {
        Long localUserId = UserHolder.getLocalUserId();
        PageBeanVo recommedUserByPage = recommendUserService.findRecommedUserByPage(localUserId, page, pagesize);
        List<RecommendUser> recommendUserList = (List<RecommendUser>) recommedUserByPage.getItems();
        ArrayList<RecommendUserVo> voList = new ArrayList<>();
        //如果为空添加几条假数据
        if (CollectionUtil.isEmpty(recommendUserList)) {
            recommendUserList = new ArrayList<>();
            for (int i = 1; i <= 6; i++) {
                RecommendUser recommendUser = new RecommendUser();
                recommendUser.setToUserId(localUserId);
                recommendUser.setUserId(Long.valueOf(i));
                recommendUser.setScore(RandomUtil.randomDouble(66, 99));
                recommendUserList.add(recommendUser);
            }
        }
        for (RecommendUser recommendUser : recommendUserList) {
            RecommendUserVo vo = new RecommendUserVo();
            Long userId = recommendUser.getUserId();
            UserInfo userInfo = userInfoService.findByUserId(userId);
            vo.setUserInfo(userInfo);
            vo.setFateValue(recommendUser.getScore().longValue());
            voList.add(vo);
        }
        recommedUserByPage.setItems(voList);
        return ResponseEntity.ok(recommedUserByPage);
    }

    public ResponseEntity personalInfo(Long jiarenId) {
        Long localUserId = UserHolder.getLocalUserId();
        RecommendUser recommendUser = recommendUserService.findByJiarenIdAndToUserId(jiarenId, localUserId);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(jiarenId);//佳人id
            recommendUser.setToUserId(localUserId);//我的id
            recommendUser.setScore(RandomUtil.randomDouble(66, 99));//随机分数
        }
        RecommendUserVo recommendUserVo = new RecommendUserVo();
        UserInfo userInfo = userInfoService.findByUserId(jiarenId);
        recommendUserVo.setUserInfo(userInfo);
        recommendUserVo.setFateValue(recommendUser.getScore().longValue());


        return ResponseEntity.ok(recommendUserVo);
    }

    @DubboReference
    private VisitorService visitorService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public ResponseEntity findVisitors() {
        Long localUserId = UserHolder.getLocalUserId();
        String lastTime = stringRedisTemplate.opsForValue().get(ConstantUtil.LAST_ACCESS_TIME + localUserId);
        //设置一个token 保存时间
        if (StrUtil.isEmpty(lastTime)) {
            stringRedisTemplate.opsForValue().set(ConstantUtil.LAST_ACCESS_TIME + localUserId, System.currentTimeMillis() + "");
        }
        List<Visitor> visitorHead5 = visitorService.findVisitorHead5(localUserId, Long.valueOf(lastTime));
        List<VisitorVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(visitorHead5)) {
            for (Visitor visitor : visitorHead5) {
                VisitorVo vo = new VisitorVo();
                UserInfo userInfo = userInfoService.findByUserId(visitor.getVisitorUserId());
                vo.setUserInfo(userInfo);
                vo.setFateValue(visitor.getScore().longValue());
                voList.add(vo);
            }
        }

        return ResponseEntity.ok(voList);
    }

    @DubboReference
    private UserLocationService userLocationService;

    public ResponseEntity searchNear(String gender, Long distance) {
        Long localUserId = UserHolder.getLocalUserId();

        List<Long> idList = userLocationService.searchNearUser(localUserId, distance);
        ArrayList<NearUserVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(idList)) {
            for (Long id : idList) {
                if (id == localUserId) {
                    continue;
                }
                UserInfo userInfo = userInfoService.findByUserId(id);
                System.out.println("附近的"+userInfo);
                if (StrUtil.equals(userInfo.getGender(), gender)) {
                    continue;
                }
                NearUserVo nearUserVo = new NearUserVo();
                nearUserVo.setUserId(id);
                nearUserVo.setAvatar(userInfo.getAvatar());
                nearUserVo.setNickname(userInfo.getNickname());
                voList.add(nearUserVo);
            }
        }
        return ResponseEntity.ok(voList);
    }
}
